package io.framework.modules.hpinfo.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import io.framework.common.annotation.LogOperation;
import io.framework.common.constant.Constant;
import io.framework.common.exception.ErrorCode;
import io.framework.common.page.PageData;
import io.framework.common.utils.ExcelUtils;
import io.framework.common.utils.Result;
import io.framework.common.validator.AssertUtils;
import io.framework.common.validator.ValidatorUtils;
import io.framework.common.validator.group.AddGroup;
import io.framework.common.validator.group.DefaultGroup;
import io.framework.common.validator.group.UpdateGroup;
import io.framework.modules.hpinfo.dto.VTHpglSpfjinfoDTO;
import io.framework.modules.hpinfo.excel.VTHpglSpfjinfoExcel;
import io.framework.modules.hpinfo.service.VTHpglSpfjinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 环境影响评价管理-环评附件信息
 *
 * @author Gujq email
 * @since 1.0.0 2020-04-13
 */
@RestController
@RequestMapping("hpinfo/thpglspfjinfo")
@Api(tags="环境影响评价管理-环评附件信息")
public class VTHpglSpfjinfoController {

    @Autowired
    private VTHpglSpfjinfoService tHpglSpfjinfoService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<VTHpglSpfjinfoDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<VTHpglSpfjinfoDTO> page = tHpglSpfjinfoService.page(params);

        return new Result<PageData<VTHpglSpfjinfoDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<VTHpglSpfjinfoDTO> get(@PathVariable("id") String id){
        VTHpglSpfjinfoDTO data = tHpglSpfjinfoService.selectById(id);

        return new Result<VTHpglSpfjinfoDTO>().ok(data);
    }

    @PostMapping("save")
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save(VTHpglSpfjinfoDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        dto.setRecordid(IdWorker.get32UUID());
        dto.setCreatetime(new Date());
        tHpglSpfjinfoService.save(dto);

        return new Result();
    }

    @GetMapping("getSpfjInfoByProjectId/{id}")
    @ApiOperation("根据建设项目ID查询-环评附件信息列表")
    public Result<List<VTHpglSpfjinfoDTO>> getProjectInfoByProjectId(@PathVariable("id") String id){

        List<VTHpglSpfjinfoDTO> data = tHpglSpfjinfoService.getSpfjInfoByProjectId(id);

        return new Result<List<VTHpglSpfjinfoDTO>>().ok(data);
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result update(VTHpglSpfjinfoDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        dto.setUpdatetime(new Date());
        tHpglSpfjinfoService.update(dto);

        return new Result();
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@PathVariable("id") String id){
        //效验数据
        AssertUtils.isBlank("id",ErrorCode.IDENTIFIER_NOT_NULL);

        tHpglSpfjinfoService.deleteById(id);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("hpinfo:thpglspfjinfo:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<VTHpglSpfjinfoDTO> list = tHpglSpfjinfoService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, VTHpglSpfjinfoExcel.class);
    }

}